using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using MCMLXVII.Tools.OrganizationChart.MembersClasses;
using MCMLXVII.BackEnd.Services.OrganizationChart.Interfaces;
using MCMLXVII.BackEnd.Services.OrganizationChart.Messages;
using System.Xml;
using MCMLXVII.Core.Common;

namespace MCMLXVII.Tools.OrganizationChart.SmartEditors
{
    public partial class ucSmartGroupEditor : ucSmartBaseEditor 
    {
        public override event EventHandler<MemberUpdatedEventsArgs> EditorClosed;
        public override event EventHandler<MemberUpdatedEventsArgs> MemberUpdated;

        private bool Loading = false;


        private MemberSecurityGroup _Member;
        private int _NodeID;
        public override int NodeID
        {
            get
            {
                return _NodeID;
            }
        }

        public ucSmartGroupEditor(MemberSecurityGroup Member, int NodeID):base()
        {
            _NodeID = NodeID;
            InitializeComponent();
            _Member = Member;
            base.CancelRequested += new EventHandler(ucSmartEditor_CancelRequested);
            base.SaveRequested += new EventHandler(ucSmartEditor_SaveRequested);
            base.pboxItemClass.Image = (System.Drawing.Image)Properties.Resources.icoGroup.ToBitmap();
        }

        void ucSmartEditor_SaveRequested(object sender, EventArgs e)
        {
            if (MemberUpdated != null)
            {
                iOrganizationTreeMemberClass EditingObject = (iOrganizationTreeMemberClass)_Member;
                msgOrganizationMemberInfo info = EditingObject.GetMemberData();
                MemberUpdated.Invoke(this, new MemberUpdatedEventsArgs(_Member.MemberID, _NodeID, EditingObject));
                base.DisableUpdate();
            }
        }

        void ucSmartEditor_CancelRequested(object sender, EventArgs e)
        {
            if (EditorClosed != null)
            {
                EditorClosed.Invoke(this, new MemberUpdatedEventsArgs(_Member.MemberID));
            }
        }


        private void ucSmartNodeEditor_Load(object sender, EventArgs e)
        {
            Loading = true;
            txtNodeID.Text = _Member.MemberID.ToString();
            txtNodeName.Text = _Member.Description;

            cmbSecurityprovider.Items.Clear();
            string[] DirectoryServices = MemberBase.DirectoryServicesNames();
            int[] DirectoryServicesIDs = MemberBase.DirectoryServicesIDs();

            for (int i = 0; i < DirectoryServices.Length; i++)
            {
                cmbSecurityprovider.Items.Add(DirectoryServices[i]);
                if (DirectoryServicesIDs[i] == _Member.SecurityProviderID)
                    cmbSecurityprovider.SelectedIndex = i;
            }
            txtLogonName.Text = _Member.LogonName;
            txtSID.Text = _Member.DirectoryIdentifier;

            foreach (msgMembershipInfo Member in _Member.Members)
            {
                grdTranslations.Rows.Add(Member.MemberID, Member.Description);
            }
            base.RelocateButtons(btnChangeGroup.Top +btnChangeGroup.Height );
            Loading = false;
        }


        private void txtNodeName_TextChanged(object sender, EventArgs e)
        {
            if (!Loading)
            {
                _Member.Description = txtNodeName.Text;
                base.EnableUpdate();
            }
        }

        private void cmbSecurityprovider_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!Loading)
            {
                if (cmbSecurityprovider.SelectedIndex > -1)
                {
                    _Member.SecurityProvider = cmbSecurityprovider.SelectedItem.ToString();
                    base.EnableUpdate();
                }
            }
        }

        private void btnSelectDir_Click(object sender, EventArgs e)
        {
            if (cmbSecurityprovider.SelectedIndex > -1)
            {
                frmDirectoryServiceUsers frmDialog = new frmDirectoryServiceUsers(txtLogonName.Text, _Member.DirectoryIdentifier, _Member.SecurityProviderID, _Member.SecurityProvider, false );
                System.Windows.Forms.DialogResult res = frmDialog.ShowDialog(Program.frm);
                if (res == System.Windows.Forms.DialogResult.OK)
                {
                    txtSID.Text = frmDialog.ID;
                    _Member.DirectoryIdentifier = frmDialog.ID;
                    _Member.LogonName = frmDialog.LogonName;
                    txtLogonName.Text = frmDialog.LogonName;
                    foreach (XmlNode node in frmDialog.PropertyMaps)
                    {
                        string NewValue = node.Attributes.GetNamedItem("Value").Value;
                        switch (node.Attributes.GetNamedItem("Name").Value.ToLower())
                        {
                            case "description": _Member.Description = NewValue;
                                txtNodeName.Text = NewValue;
                                break;
                            default:
                                break;
                        }
                    }
                    base.EnableUpdate();
                }
            }
        }

        private void btnChangeGroup_Click(object sender, EventArgs e)
        {
            frmMembersEditor frmDialog = new frmMembersEditor(_Member.Members , _Member.MemberClass , _Member.Description);
            System.Windows.Forms.DialogResult res = frmDialog.ShowDialog(Program.frm);
            if (res == System.Windows.Forms.DialogResult.OK)
            {
                _Member.Members  = frmDialog.ActualMembers;
                grdTranslations.Rows.Clear();
                foreach (msgMembershipInfo Member in _Member.Members)
                {
                    grdTranslations.Rows.Add(Member.MemberID, Member.Description);
                }
                base.EnableUpdate();
            }
        }

        private void label3_Click(object sender, EventArgs e)
        {

        }



    }
}
